package com.hctforgreen.greenservice.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.util.Log;
import android.view.KeyEvent;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.CheckVersionEntity;

import cn.dns.reader.network.Util;

public class VersionDialogUtil {

	/**
	 * 推荐更新
	 * 
	 * @param activity
	 * @param entity
	 */
	public void updateVersionRecommendDialog(final Activity activity,
			final CheckVersionEntity entity) {
		try {
			/**
			 * 弹出dialog，不能脱离Activity，所以弄try catch
			 */
			Dialog dialog = null;
			AlertDialog.Builder builder = new AlertDialog.Builder(activity);
			builder.setTitle(R.string.dialog_default_title_hint)
					.setMessage(R.string.has_new_version_recommend_dialog_content_hint)
					.setNegativeButton(R.string.dialog_cancel_hint, new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
								}
							})
					.setPositiveButton(R.string.dialog_confirm_hint, new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
									String url = HctConstants.NET_URL
											+ HctConstants.NET_VERSION_MOBILE_PATH
											+ HctConstants.GET_NEW_APP_ACTION
											+ "?" + "type=0"
											+ "&appKind=saleApp";
									// Intent intent = new Intent(
									// Intent.ACTION_VIEW, Uri.parse(url));
									// activity.startActivity(intent);
									Log.i("DATA", "APP版本下载=="+url);
									Util.dowloadApk(activity, url, "");
									Utils.submitPoints(
											activity,
											2,
											activity.getString(R.string.add_point_oper_update),
											true);
								}
							});
			dialog = builder.create();
			dialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 强制更新
	 * 
	 * @param activity
	 * @param entity
	 */
	public void updateVersionEnforceDialog(final Activity activity,
			final CheckVersionEntity entity) {

		OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				/**
				 * 拦截物理back按键
				 */
				if (keyCode == KeyEvent.KEYCODE_BACK
						&& event.getRepeatCount() == 0) {
					return true;
				} else {
					return false;
				}
			}
		};
//		View enforceTv = activity.findViewById(R.id.tv_enforce);
//		enforceTv.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// 增加空白点击事件，用于拦截下面布局的点击
//			}
//		});
//		enforceTv.setVisibility(View.VISIBLE);
		try {
			/**
			 * 弹出dialog，不能脱离Activity，所以弄try catch
			 */
			Dialog dialog = null;
			AlertDialog.Builder builder = new AlertDialog.Builder(activity);
			builder.setTitle(R.string.dialog_default_title_hint)
					.setMessage(
							R.string.has_new_version_enforce_dialog_content_hint)
					.setOnKeyListener(keyListener)
					.setPositiveButton(R.string.dialog_confirm_hint,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									// String url = HctConstants.NET_URL
									// + HctConstants.NET_VERSION_MOBILE_PATH
									// + HctConstants.GET_NEW_APP_ACTION
									// + "?" + "type=0";
									// Intent intent = new Intent(
									// Intent.ACTION_VIEW, Uri.parse(url));
									// activity.startActivity(intent);
									String url = HctConstants.NET_URL
											+ HctConstants.NET_VERSION_MOBILE_PATH
											+ HctConstants.GET_NEW_APP_ACTION
											+ "?" + "type=0";
									// Intent intent = new Intent(
									// Intent.ACTION_VIEW, Uri.parse(url));
									// activity.startActivity(intent);
									Util.dowloadApk(activity, url, "");
									Utils.submitPoints(
											activity,
											2,
											activity.getString(R.string.add_point_oper_update),
											true);
								}
							});
			dialog = builder.create();
			dialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
